%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
clear all; close all; clc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modify main_str to match path on your machine
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
main_str = 'C:\Users\Marco\Dropbox\Housing_Brainstorming\Information_disclosure';
% main_str = 'L:\agargano\Dropbox\Housing_Brainstorming\Information_disclosure';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load_str = [main_str,'\Replication_code_RFS\Model\Output\Last'];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%s
endow_sel  = 0.09; % selects value of \sigma_{\psi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n_j           = 6; % number of different values of \sigma_{\epsilon} = 0, 2%, 4%, 6%, 8%, 10%
sel_j         = 2; % select \sigma_{\epsilon} = 2%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FontSize=18;
MarkSize=9;
XlabelSize = 16;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Load

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load([load_str,'\Results_out_5_Nash_Loop_endowstd_',num2str(endow_sel),'.mat']);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sigma_eps_vec = unique(grid_mat(:,2)); % unique values of sigma_{\epsilon}
q_vec         = start_bid_scale-1; % vector of values of alpha

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Figure for calibration with \sigma_{\psi} = 9% and \sigma_{\epsilon} = 2%

for j = sel_j

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    figure(8);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    eq_Erev_vec          = eq_Erev_mat(:,j);   
    eq_EbidW_vec         = eq_EbidW_mat(:,j);
    eq_EbidW_obj_unb_vec = eq_EbidW_obj_unb_mat(:,j);
    eq_Esales_vec        = eq_Esales_mat(:,j);
    eq_Psales_vec        = eq_Psales_mat(:,j);
    eq_Nall_vec          = eq_Nall_mat(:,j);
    eq_EbidN_vec         = eq_EbidN_mat(:,j);
    eq_p_hcost0_vec      = eq_p_hcost0_mat(:,j);

    %%%%%%%%%%%%%%%
    subplot(3,2,1);
    %%%%%%%%%%%%%%%
    temp_max=NaN(length(eq_Erev_vec),1); temp_max(eq_Erev_vec==max(eq_Erev_vec))=max(eq_Erev_vec);
    temp_eq_Erev_vec=eq_Erev_vec; temp_eq_Erev_vec(eq_Erev_vec==max(eq_Erev_vec))=NaN;
    plot(q_vec,temp_eq_Erev_vec/1000,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    plot(q_vec,temp_max/1000,'*','MarkerSize',MarkSize,'MarkerFaceColor','red'); grid on; 
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    title('E[Seller Revenue]')
    ylabel('AUD 1,000');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%%
    subplot(3,2,2);
    %%%%%%%%%%%%%%%
    plot(q_vec,eq_EbidW_obj_unb_vec/1000,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;    
    plot(q_vec,eq_EbidW_vec/1000,'Linestyle','none','marker','s','MarkerSize',MarkSize,'MarkerFaceColor','red'); grid on;  
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    ylabel('AUD 1,000');
    title('E[Bidder Surplus]'); legend({'Objective','Subjective'});
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize); 
    %%%%%%%%%%%%%%%    
    subplot(3,2,3);
    %%%%%%%%%%%%%%%
    plot(q_vec,(eq_Esales_vec/1000)+650,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;    
    ylabel('AUD 1,000');
    title('E[Sale Price|Sale]')
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%% 
    subplot(3,2,4);
    %%%%%%%%%%%%%%%
    plot(q_vec,100*eq_Psales_vec,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    ylabel('(%)');
    title('P(Sale)');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%%
    subplot(3,2,5);
    %%%%%%%%%%%%%%%
    plot(q_vec,eq_Nall_vec.*eq_EbidN_vec,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    title('E[Number of Bidders]');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%%
    subplot(3,2,6);
    %%%%%%%%%%%%%%%
    plot(q_vec,100*eq_EbidN_vec,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    ylabel('(%)');
    title('E[Fraction of Bidders]');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Find Equilibrium and Law Effects for \sigma_{\psi}=9% and  Different Values of \sigma_{\epsilon}
   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Allocate Vectors for Equilibrium
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
q_opt_vec             = nan(n_j,1);
eq_Erev_opt_vec       = nan(n_j,1);
eq_EbidW_opt_vec      = nan(n_j,1);
eq_Esales_opt_vec     = nan(n_j,1);
eq_Psales_opt_vec     = nan(n_j,1);
eq_Nall_opt_vec       = nan(n_j,1);
eq_EbidN_opt_vec      = nan(n_j,1);
eq_p_hcost0_opt_vec   = nan(n_j,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Allocate Vectors for Outcomes for alpha = 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
q_zero_vec            = nan(n_j,1);
eq_Erev_zero_vec      = nan(n_j,1);
eq_EbidW_zero_vec     = nan(n_j,1);
eq_Esales_zero_vec    = nan(n_j,1);
eq_Psales_zero_vec    = nan(n_j,1);
eq_Nall_zero_vec      = nan(n_j,1);
eq_EbidN_zero_vec     = nan(n_j,1);
eq_p_hcost0_zero_vec  = nan(n_j,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
for j = 1:n_j

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    jj                      = j;
    [~,opt_ind]             = max(eq_Erev_mat(:,j)); % find equilibrium (alpha maximizes seller revenue)
    zero_ind                = q_vec==0; % find alpha = 0

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Store Equilibrium Outcomes
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    q_opt_vec(jj)            = q_vec(opt_ind); 
    eq_Erev_opt_vec(jj)      = eq_Erev_mat(opt_ind,j);
    eq_EbidW_opt_vec(jj)     = eq_EbidW_mat(opt_ind,j);    
    eq_Esales_opt_vec(jj)    = (eq_Esales_mat(opt_ind,j)/1000)+650; 
    eq_Psales_opt_vec(jj)    = eq_Psales_mat(opt_ind,j);
    eq_Nall_opt_vec(jj)      = eq_Nall_mat(opt_ind,j);
    eq_EbidN_opt_vec(jj)     = eq_EbidN_mat(opt_ind,j);
    eq_p_hcost0_opt_vec(jj)  = eq_p_hcost0_mat(opt_ind,j);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Store Outcomes for alpha = 0
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    q_zero_vec(jj)            = q_vec(zero_ind); 
    eq_Erev_zero_vec(jj)      = eq_Erev_mat(zero_ind,j);
    eq_EbidW_zero_vec(jj)     = eq_EbidW_mat(zero_ind,j);    
    eq_Esales_zero_vec(jj)    = (eq_Esales_mat(zero_ind,j)/1000)+650; 
    eq_Psales_zero_vec(jj)    = eq_Psales_mat(zero_ind,j);
    eq_Nall_zero_vec(jj)      = eq_Nall_mat(zero_ind,j);
    eq_EbidN_zero_vec(jj)     = eq_EbidN_mat(zero_ind,j);
    eq_p_hcost0_zero_vec(jj)  = eq_p_hcost0_mat(zero_ind,j);    

end    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate Changes in Expected Sales Prices and Sales Probabilities from
% Underquoting Law
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

info_Esales_mat  = nan(n_j,n_j);
info_Psales_mat  = nan(n_j,n_j);
total_Esales_mat = nan(n_j,n_j);
total_Psales_mat = nan(n_j,n_j);

for j = 1:n_j
   
   for jj = 1:j 
   
        % Expected Sales Prices
        info_Esales_mat(jj,j)   = eq_Esales_opt_vec(jj)  /eq_Esales_opt_vec(j)-1; % relative change from equilibium with sigma_{\epsilon,j} to sigma_{\epsilon,jj}
        total_Esales_mat(jj,j)  = eq_Esales_zero_vec(jj) /eq_Esales_opt_vec(j)-1; % relative change from equilibium with sigma_{\epsilon,j} to sigma_{\epsilon,jj} and alpha = 0

        % Sales Probabilities
        info_Psales_mat(jj,j)   = eq_Psales_opt_vec(jj)  - eq_Psales_opt_vec(j); % difference from equilibium with sigma_{\epsilon,j} to sigma_{\epsilon,jj}
        total_Psales_mat(jj,j)  = eq_Psales_zero_vec(jj) - eq_Psales_opt_vec(j); % difference from equilibium with sigma_{\epsilon,j} to sigma_{\epsilon,jj} and alpha = 0

   end
   
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
% Below are the two panels of Figure 9
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
figure(91);
plot(sigma_eps_vec*100,100*info_Esales_mat(:,sel_j),':r','linewidth',5); hold on;
plot(sigma_eps_vec*100,100*total_Esales_mat(:,sel_j),'-b','linewidth',5); grid on;
legend('Information Effect Only ($\Delta \sigma_{\epsilon}$)',...
       'Information Effects ($\Delta \sigma_{\epsilon}$) and Direct Effect (Increase $\alpha$ to 0)',...
       'interpreter','latex')        
ylabel('Relative Change in Price (%)')
xlabel('Post-Law $\sigma_{\epsilon}$ $(\%)$','interpreter','latex');
xlim([0,sigma_eps_vec(sel_j)*100]);
set(gca,'fontsize',36);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(92);
plot(sigma_eps_vec*100,100*info_Psales_mat(:,sel_j),':r','linewidth',5); hold on;
plot(sigma_eps_vec*100,100*total_Psales_mat(:,sel_j),'-b','linewidth',5); grid on;
ylabel('Difference in Probability (%)')
xlabel('Post-Law $\sigma_{\epsilon}$ $(\%)$','interpreter','latex');
xlim([0,sigma_eps_vec(sel_j)*100]);
set(gca,'fontsize',36);



